package com.rs.controller;

import com.rs.entity.Response;
import com.rs.entity.login.Menu;
import com.rs.entity.login.Role;
import com.rs.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author dehang
 * @date 2019/06/01
 */
@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/index")
    public String index(Model model) {
        System.out.println("111");
        return "role/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Response list(long page, long limit,String name) {
        List<Role> roles = roleService.getRoleList(page,limit,name);
        return Response.list(roles,roleService.getRoleSum(name));
    }

    @RequestMapping("/add")
    @ResponseBody
    public long addRole(String name, String remark) {

        return roleService.addRole(name, remark);
    }

    @RequestMapping("/update")
    @ResponseBody
    public void updateRole(Long id, String name, String remark) {

        roleService.updateRole(id, name, remark);
    }

    @RequestMapping("/del")
    @ResponseBody
    public void delRole(long id) {

        roleService.delRole(id);
    }

    @RequestMapping("/allocation")
    public String allocation(Model model, long id) {
        //权限已有菜单
        List<Menu> haveMenus = roleService.getHaveMenu(id);
        //一级菜单
        List<Menu> oneMenus = roleService.getOneMenu();
        //二级菜单
        List<Menu> twoMenus = roleService.getTwoMenu(haveMenus);
        model.addAttribute("one", oneMenus);
        model.addAttribute("two", twoMenus);
        model.addAttribute("hava", haveMenus);
        model.addAttribute("id",id);
        return "role/allocation";
    }

    @RequestMapping("/allot")
    @ResponseBody
    public String allot(String mid,int id){
        roleService.allot(mid,id);
        return "success";
    }

}
